<?php declare(strict_types=1);
/**
 * @author      xianganyall <xianganyall@gmail.com>
 * @copyright   2023-2025 owner
 **/

namespace Srv\Libs\ConfModel;

use Srv\Libs\Frame\Conf;

final class RouteConfGroup
{
    private array $RouteConfGroupNameList                   = [];       // RouteConf List groupName
    private array $RouteConfGroupKeyList                    = [];       // RouteConf List groupKey

    /**
     * @return array
     * getGroupNameList
     */
    public function getGroupNameList():array
    {
        $RouteConfList              = [];
        $groupNameList              = Conf::getGroupNameList(RouteConf::PREFIX);
        if(count($groupNameList) < 1) return $RouteConfList;
        foreach ($groupNameList as $groupName){
            $RouteConf              = $this->getByGroupName($groupName);
            if($RouteConf instanceof RouteConf) $RouteConfList[$groupName] = $RouteConf;
        }
        return $RouteConfList;
    }

    /**
     * @param string $groupName
     * @return RouteConf|null
     * getByGroupName
     */
    public function getByGroupName(string $groupName):?RouteConf
    {
        if(strlen($groupName) < 1) return null;
        if(isset($this->RouteConfGroupNameList[$groupName]) && $this->RouteConfGroupNameList[$groupName] instanceof RouteConf) return $this->RouteConfGroupNameList[$groupName];
        $confData                                   = Conf::getGroupList(RouteConf::PREFIX.$groupName.'.', false, true);
        if(count($confData) < 1) return null;
        $this->RouteConfGroupNameList[$groupName]   = new RouteConf($confData, $groupName);
        return $this->RouteConfGroupNameList[$groupName];
    }

    /**
     * @param string $groupKey
     * @return RouteConf|null
     * getByGroupKey
     */
    public function getByGroupKey(string $groupKey):?RouteConf
    {
        if(strlen($groupKey) < 1) return null;
        if(isset($this->RouteConfGroupKeyList[$groupKey]) && $this->RouteConfGroupKeyList[$groupKey] instanceof RouteConf) return $this->RouteConfGroupKeyList[$groupKey];
        $groupName                                  = Conf::getGroupNameListByValue(RouteConf::PREFIX, RouteConf::GROUP_KEY, $groupKey)[0]??'';
        if(!is_string($groupName) || strlen($groupName) < 1) return null;
        $confData                                   = Conf::getGroupList(RouteConf::PREFIX.$groupName.'.', false, true);
        if(count($confData) < 1) return null;
        $this->RouteConfGroupKeyList[$groupKey]     = new RouteConf($confData, $groupName);
        return $this->RouteConfGroupKeyList[$groupKey];
    }
}